package com.yanqu.road.dao.impl.activity.bulletscreen;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.bulletscreen.ActivityBarrageMessage;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivityBarrageMessageDaoImpl extends TempDataObjectDao<ActivityBarrageMessage> {

    public static String getActivityBarrageMessageSql = "select * from t_u_activity_barrage_msg where activityId=? and postTime>=? order by contentId";
    public List<ActivityBarrageMessage> getActivityBarrageMessage(int activityId, long zeroTime) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<ActivityBarrageMessage> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getActivityBarrageMessageSql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setLong(i++, zeroTime);
            rs = call.executeQuery();
            while (rs.next()) {
                ActivityBarrageMessage data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public Long getMaxContentId(int activityId) {
        String sql = "select ifnull(max(contentId),0) as maxId from t_u_activity_barrage_msg where activityId=?";
        ResultSet rs = null;
        CallableStatement call = null;
        long maxId = 0L;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }

    public static String insertActivityBarrageMessageSql = "insert into t_u_activity_barrage_msg(activityId,contentId,userId,content,postTime) values (?,?,?,?,?)";

    @Override
    protected boolean addData(ActivityBarrageMessage data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getContentId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getContent()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPostTime()));
        if (getDb().executeNoneQuery(insertActivityBarrageMessageSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateActivityBarrageMessageSql = "update t_u_activity_barrage_msg set content=?,postTime=? where activityId=? and contentId=? and userId=?";

    @Override
    protected boolean updateData(ActivityBarrageMessage data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getContent()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPostTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getContentId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateActivityBarrageMessageSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected ActivityBarrageMessage parseData(ResultSet rs) throws SQLException {
        ActivityBarrageMessage data = new ActivityBarrageMessage();
        data.setActivityId(rs.getInt("activityId"));
        data.setContentId(rs.getLong("contentId"));
        data.setUserId(rs.getLong("userId"));
        data.setContent(rs.getString("content"));
        data.setPostTime(rs.getLong("postTime"));
        return data;
    }
}
